<template>
  <div>
    <el-drawer title="新建委托合同" ref="contractAdd" :visible.sync="addShow" direction="rtl" size="35%"
               @close="dialogClose">
      <!-- <el-button type="text" class="butt" @click="hand">手动生成</el-button> -->
      <el-form ref="addForm" :model="formFileds" :rules="rules" label-width="150px" class="demo-formFileds">
        <el-form-item label="委托方类型" prop="wtflx">
          <el-select v-model="formFileds.wtflx" @change="update(formFileds.wtflx)">
            <el-option label="个人" value="个人"></el-option>
            <el-option label="企业机构" value="企业机构"></el-option>
            <el-option label="司法机构" value="司法机构"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="this.formFileds.wtflx != '司法机构'" label="委托合同类型" prop="wthtlx">
          <el-select v-model="formFileds.wthtlx">
            <el-option v-for="item in options1" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="业务类型：" prop="ywlx">
          <el-select v-model="formFileds.ywlx" clearable placeholder="请选择业务类型" @change="getpgmd">
            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="this.formFileds.wtflx == '司法机构'" label="司法委托编号" prop="sfwtbh">
          <el-input v-model="formFileds.sfwtbh"></el-input>
        </el-form-item>
        <el-form-item label="委托方" prop="wtf">
          <el-input v-model="formFileds.wtf"></el-input>
        </el-form-item>
        <el-form-item v-if="this.formFileds.wtflx == '司法机构'" label="鉴定申请人" prop="jdsqr">
          <el-input v-model="formFileds.jdsqr"></el-input>
        </el-form-item>
        <el-form-item v-if="this.formFileds.wtflx == '司法机构'" label="联系电话" prop="jdsqrPhone">
          <el-input v-model="formFileds.jdsqrPhone"></el-input>
        </el-form-item>
        <el-form-item v-if="this.formFileds.wtflx == '司法机构'" label="对方当事人" prop="dfdsr">
          <el-input v-model="formFileds.dfdsr"></el-input>
        </el-form-item>
        <el-form-item v-if="this.formFileds.wtflx == '司法机构'" label="联系电话" prop="dfdsrPhone">
          <el-input v-model="formFileds.dfdsrPhone"></el-input>
        </el-form-item>
        <el-form-item v-if="this.formFileds.wtflx == '企业机构'" label="法人/授权代表" prop="wtfFrSqdb">
          <el-input v-model="formFileds.wtfFrSqdb"></el-input>
        </el-form-item>
        <el-form-item v-if="this.formFileds.wtflx == '企业机构'" label="统一社会信用代码" prop="wtfXydm">
          <el-input v-model="formFileds.wtfXydm"></el-input>
        </el-form-item>
        <el-form-item v-if="this.formFileds.wtflx == '个人'" label="身份证号码" prop="wtfXydm">
          <el-input v-model="formFileds.wtfXydm"></el-input>
        </el-form-item>
        <el-form-item v-if="this.formFileds.wtflx != '司法机构'" label="委托方地址" prop="wtfDz">
          <el-input v-model="formFileds.wtfDz"></el-input>
        </el-form-item>
        <el-form-item v-if="this.formFileds.wtflx != '司法机构'" label="联系方式" prop="wtfLxfs">
          <el-input v-model="formFileds.wtfLxfs"></el-input>
        </el-form-item>

        <el-form-item label="银行卡号" prop="account">
          <el-input v-model="formFileds.account"></el-input>
        </el-form-item>
        <el-form-item label="开户人" prop="accountName">
          <el-input v-model="formFileds.accountName"></el-input>
        </el-form-item>
        <el-form-item label="开户行" prop="bankName">
          <el-input v-model="formFileds.bankName"></el-input>
        </el-form-item>
        <el-form-item label="评估金额" prop="money">
          <el-input type="number" v-model="formFileds.money">0.00</el-input>
          大写：{{ convertCurrency }}
        </el-form-item>

        <el-form-item label="评估目的" prop="pgmd">
          <el-select v-model="formFileds.pgmd" multiple collapse-tags>
            <el-option v-for="item in options2" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="this.formFileds.wtflx == '司法机构'" label="委托日期" prop="wtf">
          <el-date-picker v-model="formFileds.pgjzr" type="date" placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item v-if="this.formFileds.wtflx != '司法机构'" label="评估基准日" prop="pgjzr">
          <el-date-picker v-model="formFileds.pgjzr" type="date" placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="出具报告书时限" prop="cjbgsj">
          <el-input v-if="this.formFileds.wtflx == '司法机构'" v-model="formFileds.cjbgsj"
                    placeholder="正常30天最长60天"></el-input>
          <el-input v-if="this.formFileds.wtflx != '司法机构'" v-model="formFileds.cjbgsj"
                    placeholder=" 天"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit" class="pull-right margin-l-25">确定
          </el-button>
          <el-button @click="dialogClose" class="pull-right">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
import {create, getFunctions} from "@/api/evaluation/contract";
import {findByName} from '@/api/systemManage/dataDictionary';
// import { getCyyByPgmd } from "@/api/evaluation/word";
import {getCyyByAll} from '@/api/evaluation/word'

export default {
  name: "contractAdd",
  //接受父组件
  props: {
    addShow: {
      type: Boolean
    },
    qyid: {
      type: Number
    }
  },

  data() {
    return {
      options: [],
      options1: [],
      options2: [],
      formFileds: {},
      rules: {
        wthtlx: [{
          required: true,
          message: '选择委托方类型',
          trigger: 'change,blur'
        }],
        ywlx: [{
          required: true,
          message: '选择业务类型',
          trigger: 'change,blur'
        }],
        wtf: [{
          required: true,
          message: '请输入委托方',
          trigger: 'change,blur'
        }],
        wtfXydm: [{
          required: true,
          message: '请输入身份证号/统一社会信用代码',
          trigger: 'change,blur'
        }],
        // wtfDz: [{
        //     required: true,
        //     message: '请输入地址',
        //     trigger: 'change,blur'
        // }],
        wtfFrSqdb: [{
          required: true,
          message: '请输入法定代表人/授权代表',
          trigger: 'change,blur'
        }],
        // wtfLxfs: [{
        //     required: true,
        //     message: '请输入联系电话',
        //     trigger: 'change,blur'
        // }],
        pgmd: [{
          required: true,
          message: '请输入评估目的',
          trigger: 'change,blur'
        }],
        pgjzr: [{
          required: true,
          message: '请选择评估基准日',
          trigger: 'change,blur'
        }],
        cjbgsj: [{
          required: true,
          message: '请输入出具报告书时限',
          trigger: 'change,blur'
        }],
      },
      rulesCopy: {}
    }
  },
  methods: {
    // hand () {
    //     this.rules = {}
    // },
    update(value) {
      this.$forceUpdate()
      if (value == '司法机构') {
        this.rules.wthtlx[0].required = false
      } else {
        this.rules.wthtlx[0].required = true
      }
    },
    dialogClose() {
      this.$emit('dialogClose')
    },
    submit() {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          let pgmdString = ''
          this.formFileds.pgmd.forEach((item, key) => {
            if (key == 0) {
              pgmdString = pgmdString + item
            } else {
              pgmdString = pgmdString + ';' + item
            }
          })
          this.formFileds.pgmd = pgmdString
          this.formFileds.qyid = this.qyid
          create(this.formFileds).then((res) => {
            this.$emit('getList')
            this.$emit('dialogClose');
          })
        } else {
          return;
        }
      });
    },
    dataDictionaryList() {
      findByName('input=' + '委托合同类型').then((res) => {
        for (var i = 0; i < res.data.length; i++) {
          if (res.data[i].dictName == '委托合同类型')
            this.options1.push({
              label: res.data[i].dictValue,
              value: res.data[i].dictValue
            })
        }
      })
      getFunctions('qyid=' + localStorage.getItem('companyId')).then((res) => {
        for (var i = 0; i < res.data.functions.length; i++) {
          this.options.push({
            label: res.data.functions[i],
            value: res.data.functions[i]
          })
        }
      })
    },
    getpgmd() {
      this.options2 = []
      this.formFileds.pgmd = []
      getCyyByAll('input=' + this.formFileds.ywlx).then((res) => {
        for (var i = 0; i < res.data.length; i++) {
          if (res.data[i].type == this.formFileds.ywlx)
            this.options2.push({
              label: res.data[i].name,
              value: res.data[i].name,
            });
        }
      })
    }

  },
  mounted() {
    this.rulesCopy = this.rules
    this.dataDictionaryList()
    this.formFileds.wtflx = '个人'
  },
  computed:{
    convertCurrency() {
      let money = this.formFileds.money;
      //汉字的数字
      var cnNums = new Array('零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖');
      //基本单位
      var cnIntRadice = new Array('', '拾', '佰', '仟');
      //对应整数部分扩展单位
      var cnIntUnits = new Array('', '万', '亿', '兆');
      //对应小数部分单位
      var cnDecUnits = new Array('角', '分', '毫', '厘');
      //整数金额时后面跟的字符
      var cnInteger = '整';
      //整型完以后的单位
      var cnIntLast = '元';
      //最大处理的数字
      var maxNum = 999999999999999.9999;
      //金额整数部分
      var integerNum;
      //金额小数部分
      var decimalNum;
      //输出的中文金额字符串
      var chineseStr = '';
      //分离金额后用的数组，预定义
      var parts;
      // 传入的参数为空情况
      if (money == '') {
        return '';
      }
      money = parseFloat(money)
      if (money >= maxNum) {
        return ''
      }
      // 传入的参数为0情况
      if (money == 0) {
        chineseStr = cnNums[0] + cnIntLast + cnInteger;
        return chineseStr
      }
      // 转为字符串
      money = money.toString();
      // indexOf 检测某字符在字符串中首次出现的位置 返回索引值（从0 开始） -1 代表无
      if (money.indexOf('.') == -1) {
        integerNum = money;
        decimalNum = ''
      } else {
        parts = money.split('.');
        integerNum = parts[0];
        decimalNum = parts[1].substr(0, 4);
      }
      //转换整数部分
      if (parseInt(integerNum, 10) > 0) {
        let zeroCount = 0;
        let IntLen = integerNum.length
        for (let i = 0; i < IntLen; i++) {
          let n = integerNum.substr(i, 1);
          let p = IntLen - i - 1;
          let q = p / 4;
          let m = p % 4;
          if (n == '0') {
            zeroCount++;
          } else {
            if (zeroCount > 0) {
              chineseStr += cnNums[0]
            }
            zeroCount = 0;
            chineseStr += cnNums[parseInt(n)] + cnIntRadice[m];
          }
          if (m == 0 && zeroCount < 4) {
            chineseStr += cnIntUnits[q];
          }
        }
        // 最后+ 元
        chineseStr += cnIntLast;
      }
      // 转换小数部分
      if (decimalNum != '') {
        let decLen = decimalNum.length;
        for (let i = 0; i < decLen; i++) {
          let n = decimalNum.substr(i, 1);
          if (n != '0') {
            chineseStr += cnNums[Number(n)] + cnDecUnits[i]
          }
        }
      }
      if (chineseStr == '') {
        chineseStr += cnNums[0] + cnIntLast + cnInteger;
      } else if (decimalNum == '') {
        chineseStr += cnInteger;
      }
      this.formFileds.moneyCn = chineseStr;
      return chineseStr;
    }
  }
}
</script>

<style lang="less" scoped>
.select {
  width: 100%;
}

.demo-formFileds {
  padding: 10px 80px 10px 50px;
}

.butt {
  // position: relative;
  // top: -20px;
  margin-left: 80%;
  color: red;
}

/deep/ .el-drawer__header {
  background-color: #2EB3FF;
  align-items: center;
  color: #fff;
  height: 60px;
  margin-bottom: 15px;
  font-weight: bold;

}
</style>
